﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using WebCommit.Common.Entitys;
using System.Web.Routing;
using WebCommit.Common.Helpers;

namespace WebCommit.Common.Mvc.Filters
{
	public class CheckLoginAttribute :ActionFilterAttribute, IActionFilter
	{
		//public CheckLoginAttribute(ActionResult result)
		//{
		//    actionResult = result;
		//}
		//public CheckLoginAttribute()
		//{
		//        RouteValueDictionary route = new System.Web.Routing.RouteValueDictionary();
		//        route.Add("controller", "UserMgr");
		//        route.Add("action", "Login");
		//        actionResult = new RedirectToRouteResult(route);
				
		//}
		//private ActionResult actionResult;
		#region IActionFilter 成员

		public void OnActionExecuted(ActionExecutedContext filterContext)
		{
			base.OnActionExecuted(filterContext);
		}

		public void OnActionExecuting(ActionExecutingContext filterContext)
		{
			base.OnActionExecuting(filterContext);
			UserEntity user = filterContext.HttpContext.Session["LoginUser"] as UserEntity;
			if (user == null)
			{
				RouteValueDictionary route = new System.Web.Routing.RouteValueDictionary();
				route.Add("controller", "UserMgr");
				route.Add("action", "Login");
				route.Add("RedirctUrl", filterContext.HttpContext.Request.RawUrl);
				ActionResult actionResult = new RedirectToRouteResult(route);
				filterContext.Result = actionResult;
			}

		}

		#endregion
	}
}
